using System;
using System.Text;
using System.Drawing;
using cs2335.lab5;

namespace Demo_application
{
    /// <summary>
    /// Text-message retrieval client.
    /// 
    /// Connects to a text-message server and displays the text-messages
    /// for a given user.
    /// 
    /// To login to the server:
    /// Username: bgates Password: microsoft
    /// </summary>
    class DemoApp : CCTForm
    {
        /// <summary>
        /// all of the components used in the demo
        /// </summary>
        private Button connectButton;
        private TextBox password;
        private ComboBox username;
        private Label usernameLabel;
        private Label passwordLabel;
        private Label connectLabel;
        private ListBox messages;
        private ProgressBar progress;
        private Label progressLabel;
        private RadioGroup radioGroup;
        private RadioButton inbox;
        private RadioButton outbox;
        private RadioButton trash;
        private Label messagesLabel;
        private Label folderLabel;
        private DomainUpDown protocol;
        private Label protocolLabel;
        private Menu foldersMenu;
        private MenuItem inboxMenuItem;
        private MenuItem outboxMenuItem;
        private MenuItem trashMenuItem;
        private Menu fileMenu;
        private MenuItem connectMenuItem;
        private MenuItem exitMenuItem;
        private Sprite sprite;

        // how many seconds it takes to download the messages
        private int count = 5;
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        // tells whether we are connected to the server or not
        private bool connected = false;
        /// the username and password for logging in to the server
        private const string ValidUsername = "bgates";
        private const string ValidPassword = "microsoft";

        public DemoApp()
        {
            InitializeComponent();

            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_tick);
        }

        #region event functions
        void OnExitClick(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Called when the connect button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnConnectClick(object sender, EventArgs e)
        {
            if (username.Text == ValidUsername && password.Text == ValidPassword)
            {
                /// resize the screen to show the download progress bar
                this.ClientSize = new System.Drawing.Size(482, 180);
                this.progressLabel.Show();
                /// enable the timer for counting down how long it takes
                /// to download the messages
                timer.Enabled = true;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Incorrect password for account " + username.Text + ".");
            }
        }

        void OnInboxClick(object sender, EventArgs e)
        {
            if (connected)
            {
                /// empty the listbox
                messages.ClearOutListbox();

                this.messages.AddListBoxEntry("Paris Hilton:  That's hot");
                this.messages.AddListBoxEntry("Britney:  Hey! I had a great time on our date last night! Call me! ~;)");
                this.messages.AddListBoxEntry("Weird Al:  If I ever meet you, I'll control-alt-delete you.");
                this.messages.AddListBoxEntry("Britney:  Hey again! I never got a response to my last message,");
                this.messages.AddListBoxEntry("Britney:  so I just wanted to make sure you got it. Call me!");
                this.messages.AddListBoxEntry("Newegg.com:  Your Intel Core 2 Kilo has been shipped via Fedex.");
                this.messages.AddListBoxEntry("Britney:  ARE YOU IGNORING ME?");
                this.messages.AddListBoxEntry("Britney:  WHY HAVEN'T YOU CALLED ME BACK YET???");
                this.messages.AddListBoxEntry("Paris Hilton:  A tree fell through my bedroom wall last night!");
                this.messages.AddListBoxEntry("Paris Hilton:  I went to Wal-Mart to buy a new wall, but they told");
                this.messages.AddListBoxEntry("Paris Hilton:  me they told me they don't sell walls!");
                this.messages.AddListBoxEntry("Paris Hilton:  WHAT A CROCK. THAT IS FALSE ADVERTISING!!");
                this.messages.AddListBoxEntry("Britney:  I CAME BY YOUR HOUSE LAST NIGHT AT 3AM,");
                this.messages.AddListBoxEntry("Britney:  AND WAITED FOR YOU TO COME HOME,");
                this.messages.AddListBoxEntry("Britney:  BUT YOU NEVER DID. DO YOU NOT LIKE ME ANYMORE?");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Not connected.");
            }
        }

        void OnOutboxClick(object sender, EventArgs e)
        {
            if (connected)
            {
                messages.ClearOutListbox();

                this.messages.AddListBoxEntry("To Weird Al:  hmm ??");
                this.messages.AddListBoxEntry("To Paris Hilton:  Yeah, Wal-Mart is named after the creator.");
                this.messages.AddListBoxEntry("To Paris Hilton:  They don't sell walls,");
                this.messages.AddListBoxEntry("To Paris Hilton:  but I guess I can see how that would be");
                this.messages.AddListBoxEntry("To Paris Hilton:  confusing for you.");
                this.messages.AddListBoxEntry("To Britney:  I had to go out of town for the weekend.");
                this.messages.AddListBoxEntry("To Britney:  I was going to call you back, but since it looks");
                this.messages.AddListBoxEntry("To Britney:  like you're stalking me, I think I'll pass.");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Not connected.");
            }
        }

        void OnTrashClick(object sender, EventArgs e)
        {
            if (connected)
            {
                this.messages.ClearOutListbox();

                this.messages.AddListBoxEntry("Paris Hilton:  Well, where can I buy a wall at?");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Not connected.");
            }
        }
        #endregion

        /// <summary>
        /// Timer for counting down 5 seconds before showing the messages
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_tick(object sender, EventArgs e)
        {
            if (count > 0)
            {
                count--;

                progress.PerformStep();

                this.Invalidate();
            }
            else
            {
                timer.Enabled = false;
            }

            if (count == 0)
            {
                connected = true;
                /// make the window bigger, so the message window is displayed
                this.ClientSize = new System.Drawing.Size(482, 490);
                /// open the inbox message folder
                OnInboxClick(null, null);
            }
        }

        /// <summary>
        /// Put all of the components on to the screen.
        /// </summary>
        private void InitializeComponent()
        {
            this.connectButton = new Button();
            this.password = new TextBox();
            this.username = new ComboBox();
            this.usernameLabel = new Label();
            this.passwordLabel = new Label();
            this.connectLabel = new Label();
            this.messages = new ListBox(new Point(12, 316), new Size(440, 160), this);
            this.progress = new ProgressBar();
            this.progressLabel = new Label();
            this.radioGroup = new RadioGroup();
            this.inbox = new RadioButton();
            this.outbox = new RadioButton();
            this.trash = new RadioButton();
            this.messagesLabel = new Label();
            this.folderLabel = new Label();
            this.protocol = new DomainUpDown();
            this.protocolLabel = new Label();
            this.sprite = new Sprite();
            this.SuspendLayout();
            // 
            // connectButton
            // 
            this.connectButton.Location = new System.Drawing.Point(12, 60);
            this.connectButton.Name = "connectButton";
            this.connectButton.Size = new System.Drawing.Size(93, 23);
            this.connectButton.Text = "Connect";
            // 
            // password
            // 
            this.password.Location = new System.Drawing.Point(368, 60);
            this.password.Name = "password";
            this.password.Size = new System.Drawing.Size(100, 20);
            // 
            // username
            // 
            this.username.Size = new System.Drawing.Size(100, 80);
            this.username.Items.Add("bgates");
            this.username.Items.Add("sjobs");
            this.username.Items.Add("dijkstra");
            this.username.Text = "bgates";
            this.username.MultiSelect = false;
            this.username.Location = new System.Drawing.Point(187, 60);
            this.username.Name = "username";
            // 
            // usernameLabel
            // 
            this.usernameLabel.Location = new System.Drawing.Point(123, 62);
            this.usernameLabel.Name = "usernameLabel";
            this.usernameLabel.Size = new System.Drawing.Size(58, 13);
            this.usernameLabel.Text = "Username:";
            // 
            // passwordLabel
            // 
            this.passwordLabel.Location = new System.Drawing.Point(306, 62);
            this.passwordLabel.Name = "passwordLabel";
            this.passwordLabel.Size = new System.Drawing.Size(56, 13);
            this.passwordLabel.Text = "Password:";
            // 
            // connectLabel
            // 
            this.connectLabel.Location = new System.Drawing.Point(9, 35);
            this.connectLabel.Name = "connectLabel";
            this.connectLabel.Size = new System.Drawing.Size(410, 13);
            this.connectLabel.Text = "Please enter your username and password to connect to the message server:";
            // 
            // messages
            // 
            this.messages.Name = "messages";
            // 
            // progress
            // 
            this.progress.Location = new System.Drawing.Point(12, 147);
            this.progress.Name = "progress";
            this.progress.Size = new System.Drawing.Size(460, 23);
            progress.Step = 1;
            progress.Maximum = 5;
            progress.Value = 0;
            // 
            // progressLabel
            // 
            this.progressLabel.Hide();
            this.progressLabel.Location = new System.Drawing.Point(9, 131);
            this.progressLabel.Name = "progressLabel";
            this.progressLabel.Size = new System.Drawing.Size(145, 13);
            this.progressLabel.Text = "Download progress:";
            //
            // radioGroup
            //
            this.radioGroup.Location = new System.Drawing.Point(12, 200);
            this.radioGroup.Size = new Size(80, 25);
            this.radioGroup.ButtonWidth = 25;
            // 
            // inbox
            // 
            this.inbox.Name = "inbox";
            this.inbox.Text = "Inbox";
            // 
            // outbox
            // 
            this.outbox.Name = "outbox";
            this.outbox.Text = "Outbox";
            // 
            // trash
            // 
            this.trash.Name = "trash";
            this.trash.Text = "Trash";
            // 
            // messagesLabel
            // 
            this.messagesLabel.Location = new System.Drawing.Point(9, 300);
            this.messagesLabel.Name = "messagesLabel";
            this.messagesLabel.Size = new System.Drawing.Size(58, 13);
            this.messagesLabel.Text = "Messages:";
            // 
            // folderLabel
            // 
            this.folderLabel.Location = new System.Drawing.Point(9, 184);
            this.folderLabel.Name = "folderLabel";
            this.folderLabel.Size = new System.Drawing.Size(78, 13);
            this.folderLabel.Text = "Select a folder:";
            // 
            // protocol
            // 
            this.protocol.Location = new System.Drawing.Point(368, 95);
            this.protocol.Name = "protocol";
            this.protocol.Items.Add("SMS");
            this.protocol.Items.Add("N/A");
            this.protocol.Text = "SMS";
            // 
            // protocolLabel
            // 
            this.protocolLabel.Name = "protocolLabel";
            this.protocolLabel.Location = new System.Drawing.Point(270, 100);
            this.protocolLabel.Text = "Message protocol:";
            //
            // foldersMenu
            //
            foldersMenu = new Menu("Folders");
            foldersMenu.Owner = this;
            foldersMenu.Location = new Point(foldersMenu.Bounds.Width - 1, 0);
            inboxMenuItem = new MenuItem("Inbox");
            foldersMenu.AddMenuItem(inboxMenuItem);
            outboxMenuItem = new MenuItem("Outbox");
            foldersMenu.AddMenuItem(outboxMenuItem);
            trashMenuItem = new MenuItem("Trash");
            foldersMenu.AddMenuItem(trashMenuItem);
            //
            // fileMenu
            //
            fileMenu = new Menu("File");
            fileMenu.Owner = this;
            fileMenu.Location = new Point(0, 0);
            connectMenuItem = new MenuItem("Connect");
            fileMenu.AddMenuItem(connectMenuItem);
            exitMenuItem = new MenuItem("Exit");
            fileMenu.AddMenuItem(exitMenuItem);
            //
            // Sprite
            // 
            sprite.Bounds = new Rectangle(290, 20, 200, 100);
            sprite.Location = new Point(272, 190);
            //
            // Event handlers
            //
            connectButton.Click += new EventHandler(OnConnectClick);
            inbox.Click += new EventHandler(OnInboxClick);
            outbox.Click += new EventHandler(OnOutboxClick);
            trash.Click += new EventHandler(OnTrashClick);
            inboxMenuItem.Click += new EventHandler(OnInboxClick);
            outboxMenuItem.Click += new EventHandler(OnOutboxClick);
            trashMenuItem.Click += new EventHandler(OnTrashClick);
            connectMenuItem.Click += new EventHandler(OnConnectClick);
            exitMenuItem.Click += new EventHandler(OnExitClick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(482, 145);
            this.Text = "Message Client";
            this.Add(this.protocolLabel);
            this.Add(this.protocol);
            this.Add(this.folderLabel);
            this.Add(this.messagesLabel);
            this.Add(this.radioGroup);
            radioGroup.Add(this.inbox);
            radioGroup.Add(this.outbox);
            radioGroup.Add(this.trash);
            this.Add(this.progressLabel);
            this.Add(this.progress);
            this.Add(this.messages);
            this.Add(this.connectLabel);
            this.Add(this.passwordLabel);
            this.Add(this.usernameLabel);
            this.Add(this.username);
            this.Add(this.password);
            this.Add(this.connectButton);
            this.Add(this.fileMenu);
            this.Add(this.foldersMenu);
            foreach (MenuItem mi in foldersMenu.MenuItemList)
            {
                this.Add(mi);
            }
            this.Add(sprite);
            this.Name = "DemoApp";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    }
}
